//
//  MeatCityViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/31.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatCityViewController.h"
#import "MeaetCityFriendHeader.h"
#import "MeatCityFriendTableViewCell.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import <AMapFoundationKit/AMapFoundationKit.h>


#define kHeaderHeight 400


@interface MeatCityViewController ()<CLLocationManagerDelegate, MKMapViewDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) MKMapView *mapView;
@property (nonatomic, strong) CLLocation *useLocation; //当前地址
@property (nonatomic, strong) UIImageView *bigPin; //大头针

@end

@implementation MeatCityViewController

#pragma mark - lifecycle methods

- (void)setupUI{
    
}

- (void)setupLayout{
    
}

- (void)registerTableViewCell{
    
    CGFloat kTableViewHeight = KScreenHeight - 192 - TJ_TabBarHeight;
    self.tableView.height = kTableViewHeight;
    self.tableView.separatorColor = mIconLineColor;
    self.tableView.tableHeaderView = self.mapView;
    self.tableView.rowHeight = 188;

    [self.tableView registerClass:[MeatCityFriendTableViewCell class] forCellReuseIdentifier:@"MeatCityFriendTableViewCell"];

}

#pragma mark - networking methods
#pragma mark - response methods
#pragma mark - private methods

//大头针跳动的动画
- (void)bigPinAnimation{
    
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        
        CGPoint center = self.bigPin.center;
        center.y -= 20;
        [self.bigPin setCenter:center];
        
    } completion:nil];
    
    [UIView animateWithDuration:0.45 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        
        CGPoint center = self.bigPin.center;
        center.y += 20;
        [self.bigPin setCenter:center];
        
    } completion:nil];
}


#pragma mark - tableview.delegate methods
#pragma mark - tableview.datasource methods

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 55;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    
    MeaetCityFriendHeader *sectionView = [[MeaetCityFriendHeader alloc] init];

    return sectionView;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MeatCityFriendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeatCityFriendTableViewCell"];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击tableView");
}

#pragma mark - setter methods

#pragma mark - getter methods

//添加地图控件
- (MKMapView *)mapView{
    
    if (!_mapView) {
        
        _mapView = [[MKMapView alloc]initWithFrame:CGRectZero];
        _mapView.frame = CGRectMake(0, TJ_NavBarHeight, KScreenWidth, kHeaderHeight);
        _mapView.delegate = self;
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        
        if(![CLLocationManager locationServicesEnabled]||[CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse){
            [_locationManager requestWhenInUseAuthorization];
        }
        _mapView.userTrackingMode = MKUserTrackingModeFollow;
        //_mapView.mapType = MKMapTypeStandard;
        
        //大头针
        [_mapView addSubview:self.bigPin];
        
        //重新定位Icon
//        [_mapView addSubview:self.resetPositionIcon];
//        _resetPositionIcon.mj_y = _mapView.height - _resetPositionIcon.width - 15;
        
    }
    return _mapView;
}

//大头针
- (UIImageView *)bigPin
{
    if (!_bigPin) {
        
        _bigPin = [[UIImageView alloc] init];
        _bigPin.image = [UIImage imageNamed:@"address_location"];
        _bigPin.size = CGSizeMake(20, 40);
        _bigPin.mj_x = KScreenWidth/2.0 - 10;
        _bigPin.mj_y = kHeaderHeight/2.0 - 40;
    }
    return _bigPin;
}


@end
